package com.crm.action;

import com.crm.dao.ManagerDao;
import com.crm.model.role.Manager;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class ManagerAction extends ActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = 1L;
    private HttpServletRequest request;
    private ManagerDao managerDao = new ManagerDao();
    private Manager manager;
    private String error;

    /**
     * 登陆
     */
    public String login() {
        Manager currentUser = managerDao.login(manager);
        if (currentUser == null) {
            error = "用户名或密码错误";
            return ERROR;
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("currentUser", currentUser);
            return SUCCESS;
        }
    }

    public String logout() {
        request.getSession().invalidate();
        return "logout";
    }

    public Manager getManager() {
        return manager;
    }

    public void setManager(Manager manager) {
        this.manager = manager;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

}
